package com.anbow.courseware.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 统一处理返回值结果
 *
 * @author zhaoYuFei
 */
@Data
public class R<T> {

    private static Integer successCode = 200;

    private static Integer errorCode = 201;

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    //把构造方法私有
    private R() {
    }

    //成功的静态方法
    public static R ok(String msg) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(successCode);
        r.setMessage(msg);
        return r;
    }

    public static <T> R ok(String msg, T data) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(successCode);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    //失败的静态方法
    public static R error(String msg) {
        R r = new R();
        r.setSuccess(false);
        r.setCode(errorCode);
        r.setMessage(msg);
        return r;
    }

    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
